#!/bin/bash

rebuild_pgsql=$1
XDIR_parent=$(cd $(dirname $0); cd ..; pwd)

source $XDIR_parent/install_env.conf
. $XDIR_parent/util/func.sh

if [ "x$rebuild_pgsql" = "xtrue" ]; then
  echo_info "starting db service, please waiting ... ..."
  $HRS_PGSQL_HOME/bin/initdb -E UTF8 -D $HRS_PGSQL_PGDATA || { die_insh "pgsql initdb fail"; }
  echo_success "dbserver init done."
fi

echo_trace "HRS_PGSQL_PGDATA=$HRS_PGSQL_PGDATA"
sed -i "s/^#listen_addresses.*/listen_addresses='*'/" $HRS_PGSQL_PGDATA/postgresql.conf || { die_insh "sed listen_addresses fail"; }
grep -w "listen_addresses='*'" $HRS_PGSQL_PGDATA/postgresql.conf > /dev/null || { die_insh "seded listen_addresses value fail in postgresql.conf"; }

sed -i "s/^#port.*/port=${HRS_PGSQL_PORT}/" $HRS_PGSQL_PGDATA/postgresql.conf || { die_insh "sed port fail"; }
grep -w "port=${HRS_PGSQL_PORT}" $HRS_PGSQL_PGDATA/postgresql.conf > /dev/null || { die_insh "seded port value fail in postgresql.conf"; }

sed -i "s/^max_connections.*/max_connections=$HRS_MAX_CONNECTIONS/"  $HRS_PGSQL_PGDATA/postgresql.conf || { die_insh "sed max_connections fail"; }
grep -w "max_connections=$HRS_MAX_CONNECTIONS" $HRS_PGSQL_PGDATA/postgresql.conf > /dev/null || { die_insh "seded max_connections value fail in postgresql.conf"; }

sed -i "s/^shared_buffers.*/shared_buffers=$HRS_PGSQL_CONF_SHARED_BUFFERS/"  $HRS_PGSQL_PGDATA/postgresql.conf || { die_insh "sed shared_buffers fail"; }
grep -w "shared_buffers=$HRS_PGSQL_CONF_SHARED_BUFFERS" $HRS_PGSQL_PGDATA/postgresql.conf > /dev/null || { die_insh "seded shared_buffers value fail in postgresql.conf"; }

grep -w "host all all ${HRS_PGSQL_NETWORKS}  trust" $HRS_PGSQL_PGDATA/pg_hba.conf > /dev/null || echo "host all all ${HRS_PGSQL_NETWORKS}  trust"  >> $HRS_PGSQL_PGDATA/pg_hba.conf
grep -w "host all all ${HRS_PGSQL_NETWORKS}  trust" $HRS_PGSQL_PGDATA/pg_hba.conf > /dev/null || { die_insh "modify pg_hba.conf fail"; }
echo_success "dbserver configure property done."

# 创建数据库。也可以使用createdb来创建
echo_info "start service ... log : $HRS_PGSQL_LOGS/dbserver.log"
$HRS_PGSQL_HOME/bin/pg_ctl -D $HRS_PGSQL_PGDATA -l $HRS_PGSQL_LOGS/dbserver.log start || { die_insh "dbserver start fail"; }
#ps -ef|grep postgres
# 等待服务启动。
# waiting_pgsql_status 0 "start db"
waiting_proc_status $HRS_PGSQL_PORT 0 "db start"
echo_success "dbserver start done."

echo_info "starting create user [$HRS_PGSQL_DBUSER] and database [$HRS_PGSQL_DBNAME] ... ..."
#HRS_PGSQL_HOME=/home/hyrenserv/HrsDSP/hrsserver/dbserver
#HRS_PGSQL_PORT=31001
#HRS_PGSQL_DBNAME="hrsdxg"
#HRS_PGSQL_DBUSER="hrsdxg"
# bin/psql -h 127.0.0.1 -d postgres -p 31001 -c "select * from pg_database" / pg_user
$HRS_PGSQL_HOME/bin/dropdb -h 127.0.0.1 -p $HRS_PGSQL_PORT --if-exists $HRS_PGSQL_DBNAME || { die_insh "dropdb fail! cmd : $HRS_PGSQL_HOME/bin/dropdb -h 127.0.0.1 -p $HRS_PGSQL_PORT --if-exists $HRS_PGSQL_DBNAME"; }
$HRS_PGSQL_HOME/bin/dropuser -h 127.0.0.1 -p $HRS_PGSQL_PORT --if-exists $HRS_PGSQL_DBUSER || { die_insh "dropuser fail! cmd : $HRS_PGSQL_HOME/bin/dropuser -h 127.0.0.1 -p $HRS_PGSQL_PORT --if-exists $HRS_PGSQL_DBUSER"; }

#$HRS_PGSQL_HOME/bin/createuser -h 127.0.0.1 -p $HRS_PGSQL_PORT --superuser -V -P '123' $HRS_PGSQL_DBUSER
$HRS_PGSQL_HOME/bin/psql -h 127.0.0.1 -d postgres -p $HRS_PGSQL_PORT -c "create user $HRS_PGSQL_DBUSER with superuser password '$HRS_PGSQL_DBPAWD';" || { die_insh "create user fail"; }
$HRS_PGSQL_HOME/bin/psql -h 127.0.0.1 -d postgres -p $HRS_PGSQL_PORT -c "CREATE DATABASE $HRS_PGSQL_DBNAME OWNER=$HRS_PGSQL_DBUSER;" || { die_insh "create database fail"; }

mkdir -p ~/.setup/pgsql || { die_insh "mkdir [~/.setup/pgsql] fail"; }
#echo "create user $HRS_PGSQL_DBUSER with superuser password '$HRS_PGSQL_DBPAWD';" > ~/.setup/pgsql/PGInitFile
#echo "CREATE DATABASE $HRS_PGSQL_DBNAME OWNER=$HRS_PGSQL_DBUSER;" >> ~/.setup/pgsql/PGInitFile
#$HRS_PGSQL_HOME/bin/psql -h 127.0.0.1 -d postgres -p $HRS_PGSQL_PORT -f ~/.setup/pgsql/PGInitFile
#if [ $? -ne 0 ]; then
#  die_insh "create user/database fail! "
#fi

sleep 2
echo_success "create database [$HRS_PGSQL_DBNAME] for user [$HRS_PGSQL_DBUSER] DONE"
#rm -rf ~/.setup/pgsql/*

echo_info "check initdata file in [$HYREN_USERPD_INITDATA] ..."
if [ ! -f $HYREN_USERPD_INITDATA/01-create_table-mysql.sql ]; then die_insh "missing 01-create_table-mysql"; fi
if [ ! -f $HYREN_USERPD_INITDATA/02-seq.sql ]; then die_insh "missing 02-seq"; fi
if [ ! -f $HYREN_USERPD_INITDATA/03-index.sql ]; then die_insh "missing 03-index"; fi
if [ ! -f $HYREN_USERPD_INITDATA/04-insterCodes.sql ]; then die_insh "missing 04-insterCodes"; fi
if [ ! -f $HYREN_USERPD_INITDATA/05-sys_para_new.sql ]; then die_insh "missing 05-sys_para_new"; fi
echo_info "start load data ..."
$HRS_PGSQL_HOME/bin/psql -h 127.0.0.1 -U $HRS_PGSQL_DBUSER -d $HRS_PGSQL_DBNAME -p $HRS_PGSQL_PORT -f $HYREN_USERPD_INITDATA/01-create_table-mysql.sql -v ON_ERROR_STOP=on > ~/.setup/pgsql/01-create_table-mysql.log || { die_insh "fail 01-create_table-mysql"; }
echo_info "1. init table done."
$HRS_PGSQL_HOME/bin/psql -h 127.0.0.1 -U $HRS_PGSQL_DBUSER -d $HRS_PGSQL_DBNAME -p $HRS_PGSQL_PORT -f $HYREN_USERPD_INITDATA/02-seq.sql -v ON_ERROR_STOP=on > ~/.setup/pgsql/02-seq.log || { die_insh "fail 02-seq"; }
echo_info "2. load seq done."
$HRS_PGSQL_HOME/bin/psql -h 127.0.0.1 -U $HRS_PGSQL_DBUSER -d $HRS_PGSQL_DBNAME -p $HRS_PGSQL_PORT -f $HYREN_USERPD_INITDATA/03-index.sql -v ON_ERROR_STOP=on > ~/.setup/pgsql/03-index.log || { die_insh "fail 03-index"; }
echo_info "3. load idx done."
$HRS_PGSQL_HOME/bin/psql -h 127.0.0.1 -U $HRS_PGSQL_DBUSER -d $HRS_PGSQL_DBNAME -p $HRS_PGSQL_PORT -f $HYREN_USERPD_INITDATA/04-insterCodes.sql -v ON_ERROR_STOP=on > ~/.setup/pgsql/04-insterCodes.log || { die_insh "fail 04-insterCodes"; }
echo_info "4. load codes done."
$HRS_PGSQL_HOME/bin/psql -h 127.0.0.1 -U $HRS_PGSQL_DBUSER -d $HRS_PGSQL_DBNAME -p $HRS_PGSQL_PORT -f $HYREN_USERPD_INITDATA/05-sys_para_new.sql -v ON_ERROR_STOP=on > ~/.setup/pgsql/05-sys_para_new.log || { die_insh "fail 05-sys_para_new"; }
echo_info "5. load params done."

echo_success "dbserver init hyrenserv data done."
